package com.hhd.pdf.components;

import com.hhd.pdf.context.GeneratorContext;
import com.hhd.pdf.utils.PdfUtils;
import com.itextpdf.text.Font;
import com.itextpdf.text.Paragraph;
import com.itextpdf.text.pdf.PdfDiv;
import org.dom4j.Element;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Component;

import java.util.List;

@Component
public class CodeComponent extends AbstractPdfComponent {

    @Override
    public void parseComponent(Element element, GeneratorContext context) throws Exception {
        List<com.itextpdf.text.Element> elements = parseSimpleNode(element, context);
        Paragraph paragraph = createParagraph(context);

        for (com.itextpdf.text.Element element1 : elements) {
            paragraph.add(element1);
        }
        PdfDiv pdfDiv = PdfUtils.wrapDiv(paragraph, 10);
        context.getDocument().add(pdfDiv);
    }

    private static Paragraph createParagraph(GeneratorContext context) {
        Font font = context.getGeneratorParameters().getFont();
        Font font1 = new Font();
        BeanUtils.copyProperties(font, font1);
        font1.setSize(8);
        Paragraph paragraph = PdfUtils.createParagraph();
        paragraph.setFont(font);
        return paragraph;
    }

    @Override
    public String getName() {
        return "code";
    }
}
